package com.capgemini.diabeo;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;

import com.capgemini.diabeo.dummy.DummyContent;
import com.capgemini.diabeo.settings.SettingsActivity;

/**
 * An activity representing a list of Dishes. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link AddDishDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link AddDishListFragment} and the item details (if present) is a
 * {@link AddDishDetailFragment}.
 * <p>
 * This activity also implements the required
 * {@link AddDishListFragment.Callbacks} interface to listen for item
 * selections.
 */
public class AddDishListActivity extends FragmentActivity implements
AddDishListFragment.Callbacks {

	public CheckBox cbToUncheck;
	/**
	 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
	 * device.
	 */
	private boolean mTwoPane;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adddish_list);

		ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
	    
		DummyContent.setContext(this);

		if (findViewById(R.id.adddish_detail_container) != null) {
			// The detail container view will be present only in the
			// large-screen layouts (res/values-large and
			// res/values-sw600dp). If this view is present, then the
			// activity should be in two-pane mode.
			mTwoPane = true;

			// In two-pane mode, list items should be given the
			// 'activated' state when touched.
			((AddDishListFragment) getSupportFragmentManager()
					.findFragmentById(R.id.adddish_list))
					.setActivateOnItemClick(true);
		}
		
		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View vi = inflater.inflate(R.layout.listview_dishes, null);
		cbToUncheck = (CheckBox)vi.findViewById(R.id.checkbox_dish);

		// TODO: If exposing deep links into your app, handle intents here.
	}

	/**
	 * Callback method from {@link AddDishListFragment.Callbacks} indicating
	 * that the item with the given ID was selected.
	 */
	@Override
	public void onItemSelected(String id) {
		if (mTwoPane) {
			// In two-pane mode, show the detail view in this activity by
			// adding or replacing the detail fragment using a
			// fragment transaction.
			Bundle arguments = new Bundle();
			arguments.putString(AddDishDetailFragment.ARG_ITEM_ID, id);
			AddDishDetailFragment fragment = new AddDishDetailFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
			.replace(R.id.adddish_detail_container, fragment).commit();

		} else {
			// In single-pane mode, simply start the detail activity
			// for the selected item ID.
			Intent detailIntent = new Intent(this, AddDishDetailActivity.class);
			detailIntent.putExtra(AddDishDetailFragment.ARG_ITEM_ID, id);
			startActivity(detailIntent);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.choicedish, menu);
		if(MealChoice.mealChoice.equals("autre"))
		{
			menu.findItem(R.id.menu_save).setVisible(false);
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_save:
			cbToUncheck.setChecked(false);
			Intent i = new Intent(this, DishDisplay.class);
			startActivity(i);
			break;
		case R.id.action_settings:
			Intent i2 = new Intent(this, SettingsActivity.class);
			startActivity(i2);
			break;
		case android.R.id.home:
			NavUtils.navigateUpTo(this, new Intent(this,
					AddDishListActivity.class));
					return true;
		default:
			break;
		}

		return true;
	} 
}
